<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Symfony\Component\Cache\Adapter\RedisAdapter;


class QyWechatController extends Controller
{

    protected $app;
    protected $debug = false;
    protected $jsApiList = ['getContext'];
    protected $agentJsApiList = ['getContext', 'shareToExternalChat', 'getCurExternalContact', 'getCurExternalChat'];

    public function __construct(Request $request)
    {
        $qy = $request->get('qy', 'default');
        Log::info("企业代码__construct=====" . $qy);
        $this->app = \EasyWeChat\Factory::work(config('wechat.work.'.$qy));
        // 创建缓存实例
        $cache = new RedisAdapter(app('redis')->connection()->client());
        // 替换应用中的缓存
        $this->app->rebind('cache', $cache);
    }

    public function index(Request $request)
    {
        $callback = $request->get('callback');
        if (empty($callback)) return abort(403, '没有访问权限');

        $qy = $request->get('qy', 'default');
        $wechatUser = session('work.oauth_user.'.$qy);
        Log::info("企业代码=====" . $qy);
        Log::info(json_encode($wechatUser));
        if(is_null($wechatUser['userId'])) return abort(500, '数据异常');
        if ($qy == 'qijiuyiliao') {
            $this->debug = true;
        }
        $config = $this->app->jssdk->buildConfig($this->jsApiList, $this->debug, true, true, []);
        $agentConfig = $this->app->jssdk->buildAgentConfig($this->agentJsApiList, config('wechat.work.'.$qy.'.agent_id'), $this->debug, true, true, [], null);
        $contact = $this->app->user->get($wechatUser['userId']);
        Log::info("config ========>" . json_encode($config));
        Log::info("agentConfig ========>" . json_encode($agentConfig));
        return view('transfer', ['user' => $wechatUser, 'contact' => $contact, 'callback' => $callback, 'config' => $config, 'agentConfig' => $agentConfig, 'qy' => $qy]);
    }

    // public function chat($chatId, Request $request)
    // {
    //     $callback = $request->get('callback');
    //     if (empty($callback)) return abort(403, '没有访问权限');

    //     $chat = $this->app->external_contact->getGroupChat($chatId);
    //     if ($chat['errcode'] != 0) return abort(500, '获取群信息失败');
    //     $chatData = $chat['group_chat']['member_list'];
    //     return view('chat', ['chatData' => $chatData, 'callback' => $callback]);
    // }

    public function customers($chatId, Request $request)
    {
        $callback = $request->get('callback');
        if (empty($callback)) return abort(403, '没有访问权限');

        $qy = $request->get('qy', 'default');
        $wechatUser = session('work.oauth_user.'.$qy);
        if(is_null($wechatUser['userId'])) return abort(500, '数据异常');
        $contact = $this->app->user->get($wechatUser['userId']);
        $callback = preg_replace('/\userid=\d|\&userid=\d/', '', $callback);
        $callback = preg_replace('/\?&/', '?', $callback);

        return view('customers', ['chatId' => $chatId, 'contact' => $contact, 'callback' => $callback]);
    }
}
